//
//  ForumTableViewCell.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/28.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "ForumTableViewCell.h"
#import "AllCommentVC.h"
#import "UIView+Addtions.h"
#import "UIView+Addtions.h"
#import "ForumViewController.h"
#import "loginVC.h"

@interface ForumTableViewCell ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *commentLabel;
@property (weak, nonatomic) IBOutlet UILabel *likeLabel;
@property (weak, nonatomic) IBOutlet UIButton *commentBtn;
@property (weak, nonatomic) IBOutlet UIButton *likeBtn;

@end
@implementation ForumTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [_likeBtn setBackgroundImage:[UIImage imageNamed:@"xingxing_danji"] forState:UIControlStateSelected];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)customTitle:(NSString *)title likeCount:(NSInteger)likeCount commentCount:(NSInteger)commentCount{
    _titleLabel.text = title;
    _likeLabel.text = [NSString stringWithFormat:@"%ld",likeCount];
    _commentLabel.text = [NSString stringWithFormat:@"%ld",commentCount];
}
- (IBAction)commentBtnClick:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *islogined = [defaults objectForKey:CurrentUser];
    if (islogined.length>0) {
        AllCommentVC *all = [AllCommentVC new];
        all.model = self.model;
        [self.viewController.navigationController pushViewController:all animated:YES];
    }else{
        loginVC *vc = [loginVC new];
        [self.viewController.navigationController pushViewController:vc animated:YES];
        
    }
    
}
- (IBAction)likeBtnClick:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *islogined = [defaults objectForKey:CurrentUser];
    if (islogined.length>0) {

        [self likeClick];
    }else{
        loginVC *vc = [loginVC new];
        [self.viewController.navigationController pushViewController:vc animated:YES];
        
    }
}
-(void)likeClick{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"tid"] = @(self.tid);
    [HTTPManagers postLikeWithURL:@"like" params:params success:^(id json) {
        if ([json[@"message"]isEqualToString:@"unrepeatable like"]) {
            [SVProgressHUD showErrorWithStatus:@"您已点过赞～" maskType:SVProgressHUDMaskTypeBlack];
            [_likeBtn setBackgroundImage:[UIImage imageNamed:@"xingxing_dianji"] forState:UIControlStateNormal];
            _likeBtn.userInteractionEnabled = NO;
            ForumViewController *vc = (ForumViewController *)self.viewController;
            [vc.tableView reloadData];
            return ;
        }
        if ([json[@"code"]integerValue]==1) {
            [SVProgressHUD showErrorWithStatus:@"请重新登陆" maskType:SVProgressHUDMaskTypeBlack];
        }else{
            [_likeBtn setBackgroundImage:[UIImage imageNamed:@"xingxing_dianji"] forState:UIControlStateNormal];
            _likeBtn.userInteractionEnabled = NO;
            
        ForumViewController *vc = (ForumViewController *)self.viewController;
            [vc getForumData];
        }
    } failure:^(NSError *error) {
        
    }];
    
}

@end
